package com.kitchen.service.impl;

import com.kitchen.common.vo.ResStatus;
import com.kitchen.common.vo.ResultVO;
import com.kitchen.beans.entity.Comment;
import com.kitchen.beans.entity.Praise;
import com.kitchen.beans.entity.User;
import com.kitchen.mapper.CommentMapper;
import com.kitchen.mapper.PraiseMapper;
import com.kitchen.mapper.UserMapper;
import com.kitchen.service.CommentService;
import com.kitchen.beans.vo.CommentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    PraiseMapper praiseMapper;
    @Override //显示所有评论
    public ResultVO listComment(CommentVo commentVo) {
        final ResultVO resultVO=new ResultVO();
        try {
            //获取comment表中的数据
            List<CommentVo> commentVos = commentMapper.selectCommonts(commentVo.getFId());
                    Example example = new Example(Comment.class);
                    Example.Criteria criteria = example.createCriteria();
                    example.setOrderByClause("comm_praise DESC,comm_time DESC");
                    criteria.andEqualTo("commLevel", 1);

                    List<Comment> comments = commentMapper.selectByExample(example);
                    List<CommentVo> transform = transform(comments);
            for (CommentVo vo : transform) {
                User user = userMapper.selectByPrimaryKey(vo.getUserId());
                vo.setUName(user.getuUsername());
                vo.setUImg(user.getuImg());
            }
                     /**
                    * 根据上述的以查找每个一级分类相对应的二级分类
                    * 并将二级分类赋值给一级分类
                     */
            for (CommentVo comment : transform) {
                    Example example1 = new Example(Comment.class);
                    Example.Criteria criteria1 = example1.createCriteria();
                    criteria1.andEqualTo("commRoot", comment.getCommId());
                    List<Comment> comments1 = commentMapper.selectByExample(example1);
                    List<CommentVo> transform1 = transform(comments1);
                    for (CommentVo vo : transform1) {
                    User user = userMapper.selectByPrimaryKey(vo.getUserId());
                    vo.setUName(user.getuUsername());
                    vo.setUImg(user.getuImg());
                     }
                    comment.setCommentVos(transform1);
                    //获取该评论名称 头像
                for (CommentVo vo : transform1) {
                    User user = userMapper.selectByPrimaryKey(vo.getUserId());
                    vo.setUName(user.getuUsername());
                    vo.setUImg(user.getuImg());
                    //获取被回复人名字，id
                    if (vo.getToAnswerid()!=null){
                        User user1 = userMapper.selectByPrimaryKey(vo.getToAnswerid());
                        vo.setTouserName(user1.getuUsername());
                        vo.setTouserId(vo.getToAnswerid());
                    }
                }

            }
            //判断当前用户在当前作品下哪条评论有点赞
            //有则在该条评论下字段PraiseStatus里设置为1
            for (CommentVo vo : transform) {
                Boolean aBoolean = IfPraise(vo);
                if (aBoolean){
                    vo.setPraiseStatus(1);
                }
            }
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(transform);
        }catch (Exception e){
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("success");
            }
        return resultVO;
    }
    @Override //增加一条评论
    public ResultVO Commentadd(Comment comment) {
        final ResultVO resultVO=new ResultVO();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String dateStr = simpleDateFormat.format(new Date());
            comment.setCommTime(dateStr);
            comment.setToAnswerid(null);
            comment.setCommRoot(null);
            comment.setCommLevel(1);
            int insert = commentMapper.insert(comment);
            if (insert == 1) {
                resultVO.setCode(ResStatus.OK);
                resultVO.setMsg("success");
            } else {
                resultVO.setCode(ResStatus.NO);
                resultVO.setMsg("评论添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("fail");
        }
        return resultVO;
    }
    @Override //增加一条回复
    public ResultVO Commentanswer(Comment comment) {
        final ResultVO resultVO=new ResultVO();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String dateStr = simpleDateFormat.format(new Date());
            comment.setCommTime(dateStr);
            comment.setCommLevel(0);
            int insert = commentMapper.insert(comment);

            if (insert == 1) {
                resultVO.setCode(ResStatus.OK);
                resultVO.setMsg("success");
            } else {
                resultVO.setCode(ResStatus.NO);
                resultVO.setMsg("回复失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("fail");
        }

        return resultVO;
    }
    @Override //删除当前评论或回复
    public ResultVO Commentdelete(Comment comment) {
        final ResultVO resultVO=new ResultVO();
        try {
            //判断评论是否为主评论 是将根下全部评论删除
            if (comment.getCommLevel()==1){
                Example example = new Example(Comment.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("commRoot", comment.getCommId());
                int i = commentMapper.deleteByExample(example);
                if (i == 1) {
                    //删除数据库关于该评论的点赞信息
                    Example example1 = new Example(Praise.class);
                    Example.Criteria criteria1 = example1.createCriteria();
                    criteria1.andEqualTo("fId", comment.getFId());
                    criteria1.andEqualTo("commId", comment.getCommId());
                    praiseMapper.deleteByExample(example1);
                    resultVO.setCode(ResStatus.OK);
                    resultVO.setMsg("success");
                }else {
                    resultVO.setCode(ResStatus.NO);
                    resultVO.setMsg("Failed to delete main comment.");
                }
                return resultVO;

            }
            //删除次级评论
            //保留当前评论id 便于在删除后 搜索他的下一级评论
            Integer commId = comment.getCommId();
            //查询当前评论下是否还有评论
            Boolean aBoolean = IfComment(commId);
            //删除当前评论
            Example example = new Example(Comment.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("commId",commId);
            praiseMapper.deleteByExample(example);

            while (aBoolean){
                //找到已删除的评论下的那条数据
                Praise praise = praiseMapper.selectByPrimaryKey(commId);
                //保留id寻找是否还有下一条
                Integer commId1 = praise.getCommId();
                praiseMapper.deleteByPrimaryKey(commId);

              aBoolean = IfComment(commId1);
            }
        }catch (Exception e){
                e.printStackTrace();
                resultVO.setCode(ResStatus.NO);
                resultVO.setMsg("fail");
        }
        return resultVO;
    }
    @Override //点赞
    public ResultVO Commentpraise(CommentVo commentVo) {
        final ResultVO resultVO=new ResultVO();
           try {
                if (commentVo.getPraiseStatus()==1){
                    commentVo.setPraiseStatus(0);
                    //删除表中该用户在该作品下评论的点赞数据
                    Example example = new Example(Praise.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("userId",commentVo.getThisuserId());
                    criteria.andEqualTo("fId",commentVo.getFId());
                    criteria.andEqualTo("commId",commentVo.getCommId());
                    int i = praiseMapper.deleteByExample(example);
                    if (i>0){
                        //评论表里当前评论 点赞数-1
                        int update = commentMapper.updateCommentsub(commentVo.getCommId());
                        if (update==0){
                            resultVO.setCode(ResStatus.NO);
                            resultVO.setMsg("评论表点赞数据更改失败");
                        }
                        //删除成功返回该条数据
                        resultVO.setData(commentVo);
                        resultVO.setCode(ResStatus.OK);
                        resultVO.setMsg("success");
                    }else {
                        resultVO.setCode(ResStatus.NO);
                        resultVO.setMsg("fail");
                    }
                }else {
                    //没有点赞则在点赞表里加上该数据
                    Praise praise=new Praise();
                    praise.setCommId(commentVo.getCommId());
                    praise.setUserId(commentVo.getThisuserId());
                    praise.setFId(commentVo.getFId());
                    int insert = praiseMapper.insert(praise);
                    if (insert>0){
                        //添加成功则在评论表里评论数据+1
                        int update = commentMapper.updateCommentadd(commentVo.getCommId());
                        if (update==0){
                            resultVO.setCode(ResStatus.NO);
                            resultVO.setMsg("评论表点赞数据更改失败");
                        }
                        //添加成功返回该条数据
                        resultVO.setData(commentVo);
                        resultVO.setCode(ResStatus.OK);
                        resultVO.setMsg("success");
                    }else {
                        resultVO.setCode(ResStatus.NO);
                        resultVO.setMsg("fail");
                    }
                    commentVo.setPraiseStatus(1);

                }
        }catch (Exception e){
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("fail");
        }
        return resultVO;
    }
    //将商品分类转换为商品分类vo
    private List<CommentVo> transform(List<Comment> comments) {
        ArrayList<CommentVo> commentVOs = new ArrayList<>();
        for (Comment comment : comments) {
            CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
            commentVOs.add(commentVo);
        }
        return commentVOs;
        }
    //评论总点赞数
    private Integer AllPraise (List<CommentVo> commentsVo){
    int Allpraise=0;
    for (CommentVo commentVo : commentsVo) {
        Integer commPraise = commentVo.getCommPraise();
        Allpraise+=commPraise;
    }
    return  Allpraise;
 }
    //总评论数
    private Integer AllComment (List<CommentVo> commentsVo){
     int AllComment=0;
     for (CommentVo commentVo : commentsVo) {
         AllComment++;
     }
     return AllComment;
 }
    //是否点赞评论
    private Boolean IfPraise (CommentVo commentVo){
    Example example = new Example(Praise.class);
    Example.Criteria criteria = example.createCriteria();
    criteria.andEqualTo("userId",commentVo.getThisuserId());
    criteria.andEqualTo("fId",commentVo.getFId());
    criteria.andEqualTo("commId",commentVo.getCommId());
    List<Praise> praises = praiseMapper.selectByExample(example);
        for (Praise praise1 : praises) {
             if (Objects.nonNull(praise1)){
                 return true;
             }else {
                 return false;
             }
         }
     return false;
 }
    //判断评论下是否还有评论
    private Boolean IfComment (Integer commId){
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("toAnswerid",commId);
        List<Comment> comments = commentMapper.selectByExample(example);
        if (Objects.nonNull(comments)){
            return true;
        }
       return false;
    }
}
